package com.asset.dispoal.file.document.service.command;

import com.asset.dispoal.commom.page.PagingQuery;
import com.asset.dispoal.file.document.domain.db.mapper.DocumentMapper;
import com.asset.dispoal.file.document.dto.DocumentTypePageRequest;
import com.asset.dispoal.file.document.dto.DocumentTypePageResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

@Component
@Slf4j
public class DocumentTypeQueryPageCommand implements BaseCommand<DocumentTypePageRequest, Result<PageVO<DocumentTypePageResponse>>> {

    @Autowired
    private DocumentMapper documentMapper;

    @Override
    public Result<PageVO<DocumentTypePageResponse>> execute(DocumentTypePageRequest request) {
        request.setPermission((String) GlobalHeaderThreadLocal.getOrException().getExtraMap().get("permission"));
        int count = documentMapper.queryTypePageCount(request);
        Page<DocumentTypePageRequest> page = PagingQuery.getPage(count, request);
        PageVO<DocumentTypePageResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);
        if (page != null) {
            List<DocumentTypePageResponse> appointResponses = documentMapper.queryTypePage(page);
            pageVO.setResult(appointResponses);
        }
        return Result.ok(pageVO);
    }
}
